<template>
	<view class="page-privacy" v-if="popPrivacy">
		<view class="main-container">
			<view class="top-logo">
				<view class="logo-view">
					<image src="/static/mt_logo.jpg"></image>
				</view>
			</view>
			<view class="title">用户隐私保护提示</view>
			<view class="content">
				<view>感谢您使用{{miniAppName}}，您使用小程序之前应当阅读并同意</view>
				<view @click="openPrivacyContract" style="color: #AC242D;margin: 32rpx 0;">{{privacyContractName}}</view>
				<view>如您点击同意并开始使用产品服务时，即表示您已理解并同意该条款内容。如您拒绝，将无法进入小程序。</view>
			</view>
			<view class="btn-list">
				<!-- <button class="disagree-btn" type="default" @click="handleDisagree">拒绝</button> -->
				<navigator class="disagree-btn" target="miniProgram" open-type="exit">拒绝</navigator>
				<button class="agree-btn" iid="agree-btn" type="default" open-type="agreePrivacyAuthorization"
					@agreeprivacyauthorization="handleAgree">同意</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "wx-privacy",
		props: {
			showPrivacy: {
				type: Boolean,
				default: false
			}
		},
		
		data() {
			return {
				popPrivacy: false,
				miniAppName: '此小程序',
				privacyContractName: '用户隐私保护指引'
			};
		},

		created() {
			this.popPrivacy = this.showPrivacy
			if (uni.getPrivacySetting) {
				uni.getPrivacySetting({
					success: res => {
						console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
						if (res.needAuthorization) {
							var privacyContractName = res.privacyContractName
							this.privacyContractName = privacyContractName
							//因为隐私协议的名称都是《某某小程序隐私保护指引》，返回的名称包括《》，根据这个规则可以获取到小程序名称
							var lastIndex = privacyContractName.lastIndexOf("隐私保护指引》")
							this.miniAppName = privacyContractName.substring(1, lastIndex)
							uni.hideTabBar()
							this.popUp()
						} else {
						}
					},
					fail: () => {},
					complete: () => {},
				})
			} else {
			 // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
				// this.triggerEvent("agree")
			}
		},

		onLoad() {

			// wx.onNeedPrivacyAuthorization(resolve => {
			// 	// 需要用户同意隐私授权时
			// 	// 弹出开发者自定义的隐私授权弹窗
			// 	this.setData({
			// 		showPrivacy: true
			// 	})
			// 	this.resolvePrivacyAuthorization = resolve
			// })

		},

		methods: {
			openPrivacyContract() {
				uni.openPrivacyContract({
					success: res => {
						console.log('openPrivacyContract success')
					},
					fail: res => {
						console.error('openPrivacyContract fail', res)
					}
				})
			},
			
			popUp() {
				this.popPrivacy = true
			},
			
			disPopUp() {
				this.popPrivacy = false
			},
			
			handleAgree() {
				this.disPopUp()
				this.$emit('agreePrivacy')				
			},
			
			handleDisagree() {
				this.disPopUp()
				
			}
		}
	}
</script>

<style scoped>
	.page-privacy {
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100vh;
		background-color: #fff;
		z-index: 999;
	}

	.main-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx 32rpx;
		width: 90%;
		line-height: 54rpx;
		border-radius: 24rpx;
		background-color: #EFEFEF;
		position: relative;
	}
	
	.top-logo{
		position: absolute;
		top: -88rpx;
		display: flex;
		align-items: center;
	}

	.logo-view{
		padding: 4rpx;
		border-radius: 20rpx;
		background-color: #efefef;
	}
	
	.logo-view image{
		border-radius: 16rpx;
		width: 100rpx;
		height: 100rpx;
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 32rpx;
	}

	.content {
		font-size: 32rpx;
	}

	.btn-list {
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 100%;
		margin-top: 40rpx;
	}

	.disagree-btn,
	.agree-btn {
		margin: 0;
		width: 260rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 36rpx;
		border-radius: 10rpx;
	}
	
	.disagree-btn {
		background-color: #f8f8f8;
		color: #000;
	}

	.agree-btn {
		color: #fff;
		background-color: #AC242D;
	}
</style>
